package me.leon.ade;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

/** Created by Administrator on 2018/4/17. */
public abstract class AbsWindow {
    protected WindowManager.LayoutParams params;
    protected Context app;
    protected WindowManager wm;
    protected View view;
    protected LayoutInflater inflater;

    protected OnFloatWindowClickListener listener;
    protected OnLongClickListener longClickListener;

    public WindowManager getWm() {
        return wm;
    }

    public View getView() {
        return view;
    }

    public void setListener(OnFloatWindowClickListener listener) {
        this.listener = listener;
    }

    public void setLongListener(OnLongClickListener listener) {
        this.longClickListener = listener;
    }

    public void show(Context application) {
        app = application;
        if (inflater == null) {
            inflater = LayoutInflater.from(app);
        }
        params = new WindowManager.LayoutParams();
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;

        if (isOverlayApiDeprecated()) {
            params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {

            if (Build.VERSION.SDK_INT < 21) {
                params.type = WindowManager.LayoutParams.TYPE_PRIORITY_PHONE;
            } else if (Build.VERSION.SDK_INT < 24) {
                params.type = WindowManager.LayoutParams.TYPE_TOAST;
            } else {
                params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
            }
        }
        params.flags =
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                        | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
        params.format = PixelFormat.TRANSLUCENT;

        customLayoutParameter();
        app = application;
        wm = (WindowManager) application.getSystemService(Context.WINDOW_SERVICE);

        view = inflater.inflate(bindLayout(), null);
        initView();
        wm.addView(view, params);
    }

    protected abstract void initView();

    public abstract int bindLayout();

    public void customLayoutParameter() {}

    public void removeView() {
        if (wm != null && view != null) {
            wm.removeViewImmediate(view);
            view = null;
            wm = null;
        }
    }

    public void updateLocation(int x, int y) {
        params.y = y;
        params.x = x;
        params.gravity = Gravity.START | Gravity.TOP;
        wm.updateViewLayout(view, params);
    }

    protected boolean isOverlayApiDeprecated() {
        return Build.VERSION.SDK_INT >= 26;
    }

    public interface OnFloatWindowClickListener {
        void onClick();
    }

    public interface OnLongClickListener {
        void onLongClick();
    }
}
